.PHONY: init build run-bin dev-run docker-build docker-run clean tidy postgres-up postgres-down replace-module setup-mysql setup-mysql-win

# 设置 SHELL 为 bash
SHELL := /bin/bash

# 默认目标
all: init build run-bin

# 初始化项目
init:
	@echo "Initializing project..."
	go mod tidy
	go mod verify

# 构建项目
build: init
	@echo "Building project..."
	go build -o bin/main main.go

# 运行二进制文件
run-bin: build
	@echo "Running binary..."
	./bin/main

# 停止 PostgreSQL 容器
postgres-down:
	@echo "Stopping PostgreSQL container..."
	docker-compose down postgres

# 启动 PostgreSQL 容器
postgres-up:postgres-down
	@echo "Starting PostgreSQL container..."
	docker-compose up -d postgres
	@echo "Waiting for PostgreSQL to be ready..."
	@for i in 1 2; do \
		docker-compose exec -T postgres pg_isready -h localhost -U postgres && break || \
		echo "Waiting for PostgreSQL ($$i)..." && \
		sleep 2; \
	done

# 直接使用 go run 运行 (开发模式)
dev-run: init postgres-up
	@echo "Running with go run..."
	go run main.go

# 使用 air 实时重载运行 (需要先安装 air)
air-run: init postgres-up
	@echo "Running with air..."
	air

# 构建 Docker 镜像
docker-build:
	@echo "Building Docker image..."
	docker-compose build

# 运行 Docker 容器
docker-run: docker-build
	@echo "Running Docker containers..."
	docker-compose up -d

# 停止并删除 Docker 容器
docker-stop:
	@echo "Stopping Docker containers..."
	docker-compose down

# 清理构建文件
clean:
	@echo "Cleaning build files..."
	rm -rf bin/
	go clean

# 更新依赖
tidy:
	@echo "Updating dependencies..."
	go mod tidy

# 开发模式：运行并监视文件变化
dev: dev-run

# 安装开发工具
install-tools:
	@echo "Installing development tools..."
	go install github.com/cosmtrek/air@latest

# 运行方式说明
run-help:
	@echo "Running methods:"
	@echo "1. make run-bin   - Build and run binary (production)"
	@echo "2. make dev-run   - Run with 'go run' (development)"
	@echo "3. make air-run   - Run with air (live reload)"
	@echo "4. make docker-run - Run in Docker container"

# 替换模块名命令
replace-module:
	@echo "Starting module replacement..."
	@if [ "$(OS)" = "Windows_NT" ]; then \
		powershell -Command "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process; [Console]::OutputEncoding = [System.Text.Encoding]::UTF8; .\replace_module.ps1"; \
	else \
		export LANG=en_US.UTF-8; \
		export LC_ALL=en_US.UTF-8; \
		bash ./replace_module.sh; \
	fi
	@echo "Module replacement completed"

# 可以添加其他常用命令
run:
	go run main.go

build:
	go build -o app

clean:
	rm -f app 


# 帮助信息
help:
	@echo "Available commands:"
	@echo "  make init          - Initialize project and install dependencies"
	@echo "  make build        - Build the project"
	@echo "  make run-bin      - Build and run the binary"
	@echo "  make dev-run      - Run with 'go run' (development)"
	@echo "  make air-run      - Run with air (live reload)"
	@echo "  make docker-build - Build Docker image"
	@echo "  make docker-run   - Run Docker containers"
	@echo "  make docker-stop  - Stop Docker containers"
	@echo "  make postgres-up  - Start PostgreSQL container"
	@echo "  make postgres-down - Stop PostgreSQL container"
	@echo "  make clean        - Clean build files"
	@echo "  make tidy         - Update dependencies"
	@echo "  make install-tools - Install development tools"
	@echo "  make help         - Show this help message"
	@echo "  make replace-module - Replace module name in all .go files"

# MySQL 相关命令
setup-mysql:
	@echo "Setting up MySQL database..."
	@bash scripts/setup-mysql.sh

setup-mysql-win:
	@echo "Setting up MySQL database on Windows..."
	@powershell -ExecutionPolicy Bypass -File scripts/setup-mysql.ps1

# 启动独立容器
run-alone:
	docker build -t user-crud-app-alone .
	docker run -d --name user-crud-app-alone -p 8081:8081 \
		-e DB_TYPE=mysql \
		-e DB_HOST=host.docker.internal \
		-e DB_PORT=3306 \
		-e DB_USER=dynamic_user \
		-e DB_PASSWORD=dynamic_password \
		-e DB_NAME=dynamic_db \
		--add-host=host.docker.internal:host-gateway \
		user-crud-app-alone

# 停止并删除独立容器
stop-alone:
	docker stop user-crud-app-alone || true
	docker rm user-crud-app-alone || true